package org.yanghaokun.pet.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yanghaokun.basic.util.AjaxResult;
import org.yanghaokun.basic.util.LoginContext;
import org.yanghaokun.basic.util.PageList;
import org.yanghaokun.order.domain.AdoptOrder;
import org.yanghaokun.order.query.AdoptOrderQuery;
import org.yanghaokun.order.service.IAdoptOrderService;
import org.yanghaokun.pet.domain.Pet;
import org.yanghaokun.pet.query.PetQuery;
import org.yanghaokun.pet.service.IPetService;
import org.yanghaokun.product.query.ProductQuery;
import org.yanghaokun.user.domain.LoginInfo;
import org.yanghaokun.user.domain.User;
import org.yanghaokun.user.service.IUserService;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;
    @Autowired
    private IAdoptOrderService adoptOrderService;
    @Autowired
    private IUserService userService;

    @PostMapping
    public PageList<Pet> pet(@RequestBody PetQuery query){
        return petService.getList(query);
    }

    @PostMapping("/list")
    public PageList<Pet> petList(@RequestBody PetQuery query){
        return petService.getList(query);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        try {
            petService.onsaleOrOffsale(1,ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败" + e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        try {
            petService.onsaleOrOffsale(0,ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败" + e.getMessage());
        }
    }

    @GetMapping("/{petId}")
    public Pet getOne(@PathVariable(value = "petId") Long petId) {
            return petService.findOne(petId);
    }

    @GetMapping("/adopt/{petId}")
    public AjaxResult adopt(@PathVariable(value = "petId") Long petId, HttpServletRequest request) {
        try {
            //需要用户的信息，才能知道是谁领养了
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            petService.adopt(petId,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("领养失败" + e.getMessage());
        }
    }

    /**
     * 根据用户查找领养的宠物
     * @param query
     * @return
     */
    @PostMapping("/user")
    public PageList<AdoptOrder> query(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        LoginInfo info = LoginContext.getLoginInfo(request);
        User user = userService.findOneByLoginInfoId(info.getId());
        //我们是查看那个用户领养的宠物所以我们就在这绑定用户id
        query.setUserId(user.getId());
        //去看了前端界面，每页我们需要展示9条数据，所以只需要赋初始值9
        query.setPageSize(9);
        return adoptOrderService.getList(query);
    }
}
